module AboutYou
  module SDK
    module Model
      ###
      # this class represents a products ean result model
      ###
      class ProductsEansResult
        # Array of not found eans
        attr_accessor :eans_not_found
        # String page hash
        attr_accessor :page_hash
        # Array of errors
        attr_accessor :errors
        # Array of instances of AboutYou::SDK::Model::Product
        attr_accessor :products

        ###
        # This method is used for creating an instance of this class by a json_object.
        #
        # * *Args*    :
        #   - +json_object+ -> the json_object received from the api
        #   - +factory+ -> instance of AboutYou::SDK::Factory::DefaultModelFactory
        #
        # * *Returns* :
        #   - Instance of AboutYou::SDK::Model::ProductsEansResult
        ###
        def self.create_from_json(json_object, factory)
          products_ean_result = new
          products_ean_result.products = []
          products_ean_result.eans_not_found = []
          products_ean_result.errors = []

          products_ean_result.page_hash = json_object['page_hash'] ? json_object['page_hash'] : nil

          json_object['eans'].each do |json_product|
            if json_product['error_code']
              products_ean_result.errors.push(json_product)
              products_ean_result.eans_not_found.push(json_product['ean'])
              next
            end
            products_ean_result.products.push(factory.create_product(json_product))
          end if json_object['eans']

          products_ean_result
        end
      end
    end
  end
end
